package com.resou.reader.mine.vipcenter.more;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.PlateMoreBean;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.service.VipService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.data.bookstore.model.PlateNovels;

import io.reactivex.functions.Consumer;

/**
 * 查看更多Presenter层
 */
public class MonthlyMorePresenter extends ResouBasePresenter<IMonthlyMoreContract.IMonthlyMoreView> implements IMonthlyMoreContract.IMonthlyMorePresenter {
    VipService vipService;

    public MonthlyMorePresenter(IMonthlyMoreContract.IMonthlyMoreView view) {
        super(view);
        vipService = ApiImp.getInstance().getService(VipService.class);
    }


    /***
     *请求更多数据
     * @param plateId       板块ID：榜单列表接口返回的plateId
     * @param plateViewType 查看类型：榜单列表接口返回的plateViewType
     * @param page          页数：从0开始，默认为0
     */
    @Override
    public void quearMonthlyMore(String plateId, String plateViewType, int page) {
        mView.showProgress();
        vipService.getVIPPlateMore(plateId, plateViewType, page, pageSize)
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Consumer<Result<PlateMoreBean<PlateNovels>>>() {
                    @Override
                    public void accept(Result<PlateMoreBean<PlateNovels>> plateMoreBeanResult) throws Exception {
//                        plateMoreBeanResult.getData().getPlateMore();
                        if (page == 0) {
                            mView.getSmartRefreshLayout().finishRefresh();
                        }
                        if (plateMoreBeanResult.getData().getPlateMore().size() < 15) {
                            mView.getSmartRefreshLayout().finishLoadMoreWithNoMoreData();
                        } else {
                            mView.getSmartRefreshLayout().finishLoadMore();
                        }
                        mView.showMonthlyMoreData(plateMoreBeanResult.getData().getPlateMore());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showError(throwable);
                    }
                });

    }
}
